<?php
class Model_cp extends Model
{
	function Model_cp()
	{
		parent::Model();
		$this->load->database();
	}
	function _lang(){
		$lang = $this->session->userdata('lang');
		$this->db->where('ngonngu',$lang);
	}
	function get_trangthaiwebsite(){
		$this->db->limit(1);
		$query = $this->db->get('tblmessage');
		return $query->row();
	}
	function get_config($tukhoa=''){
		$this->db->where('tukhoa',$tukhoa);
		$query = $this->db->get('tblconfig');
		return $query->row();
	}
	
	function check_email($email=''){
		$this->db->where('email',$email);
		$query = $this->db->from('tbluser');
		return $query->count_all_results();
	}
	
	function check_taikhoan($taikhoan=''){
		$this->db->where('taikhoan',$taikhoan);
		$query = $this->db->from('tbluser');
		return $query->count_all_results();
	}
	
	function insert_user($taikhoan, $matkhau, $hoten, $email, $diachi, $dienthoai, $quyen=2, $active=1){
		$data = array(
					  'taikhoan'=>$taikhoan,
					  'matkhau'=>md5($matkhau),
					  'hoten'=>$hoten,
					  'email'=>$email,
					  'diachi'=>$diachi,
					  'dienthoai'=>$dienthoai,
					  'quyen'=>$quyen,
					  'active'=>$active,
					  );
		$this->db->insert('tbluser',$data);
	}
	function check_dangnhap($taikhoan, $matkhau){
		$this->db->where('taikhoan',$taikhoan);
		$this->db->where('matkhau',md5($matkhau));
		$query = $this->db->get('tbluser');
		return $query->row();
	}
	function get_thongtin_user_ten($taikhoan=''){
		$this->db->where('taikhoan',$taikhoan);
		$query = $this->db->get('tbluser');
		return $query->row();
	}
	
	function get_thongtin_user_id($id=0){
		$this->db->where('id',$id);
		$query = $this->db->get('tbluser');
		return $query->row();
	}
	//Album --------------------------
	function get_album(){
		$this->db->where('active',1);
		$this->db->order_by('thutu');
		$query = $this->db->get('tblalbum');
		return $query->result();
	}
	function get_hinhanhalbum($danhmuc=0, $limit=0, $offset=0){
		$this->db->where('active',1);
		$this->db->where('danhmuc',$danhmuc);
		if($limit>0)
			$this->db->limit($limit, $offset);
		$query = $this->db->get('tblhinhanhalbum');
		return $query-result();
	}
	//#Album --------------------------
	
	//TIN TUC -------------------------
	function get_danhmuctintuc($danhmuc=0){
		$this->_lang();
		$this->db->where('danhmuc',$danhmuc);
		$this->db->where('active',1);
		$this->db->order_by('thutu');
		$query = $this->db->get('tbldanhmuctintuc');
		return $query->result();
	}
	function get_tintuc($danhmuc=0){
		$this->_lang();
		if($danhmuc!=0)
			$this->db->where('danhmuc',$danhmuc);
		$this->db->where('active',1);
		$query = $this->db->get('tbltintuc');
		return $query->result();
	}
	//#TIN TUC -------------------------
	
	//GIỚI THIỆU -----------------------
	function get_gioithieu($limit=1){
		$this->_lang();
		$this->db->where('active',1);
		if($limit==1)
			$this->db->limit($limit);
		$query = $this->db->get('tblgioithieu');
		if($limit==1)
			return $query->row();
		else
			return $query->result();
	}
	//#GIỚI THIỆU ----------------------
	
	//HỖ TRỢ ONLINE -----------------------
	function get_hotroonline($danhmuc=0){
		$this->db->where('active',1);
		$this->db->where('danhmuc',$danhmuc);
		$this->db->order_by('thutu');
		$query = $this->db->get('tblhotroonline');
		return $query->result();
	}
	//#HỖ TRỢ ONLINE ----------------------
	
	//QUẢNG CÁO --------------------------
	
	function get_quangcao($vitri=0){
		if($vitri!=0)
			$this->db->where('vitri',$vitri);
		$this->db->where('active',1);
		$this->db->order_by('thutu');
		$query = $this->db->get('tblquangcao');
		return $query->result();
	}
	//#QUẢNG CÁO --------------------------
	
	//SLIDE SHOW------------------------------
	function get_slide(){
		 $this->db->where('active',1);
		 $this->db->order_by('thutu');
		 $query = $this->db->get('tblslide');
		 return $query->result();
	}
	//#SLIDE SHOW------------------------------
	
	
	/*COUNTER -------------------------------*/
	function total_counter(){
//		$query = "SELECT MAX(id) AS total FROM counter";
		$this->db->limit(1);
		$this->db->order_by('id','desc');
		$query = $this->db->get('counter');
		return $query->row();
	}
	function xoacounter($temp){
		$query = "DELETE FROM counter WHERE id<'$temp'";
		$this->db->query($query);
	}
	function count_visitip_counter($ip, $locktime, $now){
		//$this->db->where('ip',$ip);
//		$tm_temp = 
	//	$this->db->where('tm',$locktime);
		$sql = "SELECT COUNT(*) AS visitip FROM counter WHERE ip='$ip' AND (tm+'$locktime')>'$now'";
		$query = $this->db->query($sql);
		return $query->row()->visitip;
	}
	function insert_counter($now, $ip){
		$data = array(
					  'tm'=>$now,
					  'ip'=>$ip
					  );
		$this->db->insert('counter',$data);
	}
	function get_todayrecord($daystart){
		$this->db->where('tm >',$daystart);
		$query = $this->db->from('counter');
		return $query->count_all_results();
/*		$query = $this->db->query("SELECT COUNT(*) AS todayrecord FROM counter WHERE tm>'$daystart'");
		return $query;//->todayrecord;
*/	}
	function get_count_yesterdayrec($yesterdaystart, $daystart){
		//$query = $this->db->query("SELECT COUNT(*) AS yesterdayrec FROM counter WHERE tm>'$yesterdaystart' and tm<'$daystart'");
		$this->db->where('tm >',$yesterdaystart);
		$this->db->where('tm <',$daystart);
		$query = $this->db->from('counter');
		return $query->count_all_results();
	}
	function count_weekrec($weekstart){
		$this->db->where('tm >',$weekstart);
		$query = $this->db->from('counter');
		return $query->count_all_results();
		/*$sql = "SELECT COUNT(*) AS weekrec FROM counter WHERE tm>='$weekstart'";
		$query = $this->db->query($sql);
		return $query;*/
	}
	function count_monthrec($monthstart){
		$this->db->where('tm >',$monthstart);
		$query = $this->db->from('counter');
		return $query->count_all_results();
		/*$sql = "SELECT COUNT(*) AS monthrec FROM counter WHERE tm>='$monthstart'";
		$query = $this->db->query($sql);
		return $query;*/
	}
	function insert_songuoionline($session, $time){
		$data = array(
					  'session'=>$session,
					  'time'=>$time
					  );
		$this->db->insert('user_online',$data);
	}
	function update_nguoionline($time, $session){
		$data = array(
					  'time'=>$time,
					  'session'=>$session
					  );
		$this->db->update('user_online',$data);
	}
	function get_count_truycap(){
		$query = $this->db->from('user_online');
		return $query->count_all_results();
	}
	function delete_truycap($time_check){
		$this->db->where('time <',$time_check);
		$this->db->delete('user_online');
	}
	function count_session_online($session){
		$this->db->where('session',$session);
		$query = $this->db->from('user_online');
		return $query->count_all_results();
	}
	
	
	/*/COUNTER------------------------------*/
	function get_topmenu(){
		$this->db->where('active',1);
		$this->db->order_by('thutu');
		$query  = $this->db->get('tbltopmenu');
		return $query->result();
	}
	function sanphamhot($danhmuc=0, $limit=0, $offset){
		if($danhmuc>0)
			$this->db->where('danhmuc', $danhmuc);
		if($limit>0)
			$this->db->limit($limit, $offset);
		$this->db->order_by('thutu');
		$this->db->where('hot',1);
		$this->db->where('active',1);
		$query = $this->db->get('tblsanpham');
		return $query->result();
	}
	function get_danhmucsanpham($danhmuc=0){
		$this->db->where('active',1);
		$this->db->where('danhmuc', $danhmuc);
		$this->db->order_by('thutu');
		$query = $this->db->get('tbldanhmucsanpham');
		return $query->result();
	}
	function chitietdanhmucsanpham($id){
		$this->db->where('id', $id);
		$query = $this->db->get('tbldanhmucsanpham');
		return $query->row();
	}
	function chitietsanpham($id){
		$this->db->where('id', $id);
		$query = $this->db->get('tblsanpham');
		return $query->row();
	}
	function get_baiviet_dautien_of_danhmuc($danhmuc=0){
		if($danhmuc>0)
			$this->db->where('danhmuc', $danhmuc);
		$this->db->limit(1);
		$this->db->order_by('thutu');
		$this->db->where('active',1);
		$query = $this->db->get('tblbaiviet');
		return $query->row();
	}
	function get_sanpham_trangchu($danhmuc=0, $limit=0, $offset=0){
		if($danhmuc>0)
			$this->db->where('danhmuc',$danhmuc);
		if($limit>0)
			$this->db->limit($limit, $offset);
		$this->db->where('active',1);
		$this->db->where('trangchu',1);
		$this->db->order_by('thutu');
		$query = $this->db->get('tblsanpham');
		return $query->result();
	}
	function get_count_sanpham_trangchu($danhmuc=0){
		if($danhmuc>0)
			$this->db->where('danhmuc',$danhmuc);
		$this->db->where('active',1);
		$this->db->where('trangchu',1);
		$query = $this->db->from('tblsanpham');
		return $query->count_all_results();
	}
	function get_all_sanpham($danhmuc=0, $limit=0, $offset=0){
		if($danhmuc>0)
			$this->db->where('danhmuc',$danhmuc);
		if($limit>0)
			$this->db->limit($limit, $offset);
		$this->db->where('active',1);
		//$this->db->where('trangchu',1);
		$this->db->order_by('thutu');
		$query = $this->db->get('tblsanpham');
		return $query->result();
	}
	function get_count_allsanpham($danhmuc=0){
		if($danhmuc>0)
			$this->db->where('danhmuc',$danhmuc);
		$this->db->where('active',1);
		//$this->db->where('trangchu',1);
		$query = $this->db->from('tblsanpham');
		return $query->count_all_results();
	}
	function get_baiviet($danhmuc=0, $limit=0, $offset=0){
		if($danhmuc>0)
			$this->db->where('danhmuc', $danhmuc);
		if($limit>0)
			$this->db->limit($limit, $offset);
		$this->db->where('active',1);
		$this->db->order_by('id', 'desc');
		$query = $this->db->get('tblbaiviet');
		return $query->result();
	}
	function get_count_baiviet($danhmuc=0){
		if($danhmuc>0)
			$this->db->where('danhmuc', $danhmuc);
		$this->db->where('active',1);
		$query = $this->db->from('tblbaiviet');
		return $query->count_all_results();
	}
	function chitietbaiviet($id){
		$this->db->where('id', $id);
		$query = $this->db->get('tblbaiviet');
		return $query->row();
	}
	function get_tinkhac_baiviet($danhmuc=0, $id=0, $limit=0){
		if($danhmuc>0)
			$this->db->where('danhmuc', $danhmuc);
		if($limit>0)
			$this->db->limit($limit);
		if($id>0)
			$this->db->where('id <', $id);
		$this->db->where('active',1);
		$this->db->order_by('id', 'desc');
		$query = $this->db->get('tblbaiviet');
		return $query->result();
	}
	function chitiet_topmenu($id){
		$this->db->where('id', $id);
		$query = $this->db->get('tbltopmenu');
		return $query->row();
	}
	function get_baiviet_trangchu($danhmuc=0, $limit=0){
		if($danhmuc>0)
			$this->db->where('danhmuc', $danhmuc);
		if($limit>0)
			$this->db->limit($limit);
		$this->db->where('active',1);
		$this->db->where('trangchu',1);
		$this->db->order_by('thutu');
		$query = $this->db->get('tblbaiviet');
		return $query->result();
	}
	function get_hinhanh($limit=0){
		if($limit>0)
			$this->db->limit($limit);
		$this->db->where('active',1);
		$this->db->order_by('thutu');
		$query = $this->db->get('tblhinhanh');
		return $query->result();
	}
	function search($tukhoa=''){
		$this->db->like('tieude', $tukhoa);
		$this->db->where('active',1);
		$query = $this->db->get('tblsanpham');
		return $query->result();
	}
	function sanphamkhac($danhmuc, $id, $limit){
		$this->db->where('active',1);
		if($danhmuc>0)
			$this->db->where('danhmuc', $danhmuc);
		$this->db->where('id !=', $id);
		if($limit>0)
			$this->db->limit($limit);
		$this->db->order_by('thutu');
		$query = $this->db->get('tblsanpham');
		return $query->result();
	}
}
?>